package org.chen.mvc.factory;

import org.chen.mvc.annotation.Controller;
import org.chen.spring.ChenApplicationContext;
import org.chen.spring.core.factory.BeanFactory;
import org.chen.spring.core.factory.ScanBean;

import java.lang.annotation.Annotation;

/**
 * 控制器扫描
 *
 * @author Chen
 * @since 2023/6/7 20:48
 */
public class ControllerScanBean extends ScanBean {

    private final ControllerFactory controllerFactory = ChenApplicationContext.getBeanFactory().getBean(ControllerFactory.class);

    public ControllerScanBean(String componentScan) {
        super(componentScan);
    }

    @Override
    public void assembleBeanDefinition(Class<?> bean, Annotation annotation) {
        Class<? extends Annotation> annotationType = annotation.annotationType();
        if (annotationType == Controller.class) {
            controllerFactory.register(bean);
            String value = ((Controller) annotation).value();
            BeanFactory.registerBean("".equals(value) ? bean.getSimpleName() : value, bean);
        }
    }
}
